package com.example.project3;

import static com.example.project3.Calculator.calculate;
import static com.example.project3.Calculator.expressionToList;
import static com.example.project3.Calculator.parseToSuffixExpression;

import android.app.Application;
import android.content.Context;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.example.project3.databinding.ActivityMainBinding;

import java.util.List;

public class CalculatorViewModel extends AndroidViewModel {

    private MutableLiveData<String> input;
    private MutableLiveData<String> result;

    private String expression="";

    public CalculatorViewModel(@NonNull Application application) {
        super(application);
        this.input = new MutableLiveData<>();
        this.result = new MutableLiveData<>();
        this.input.setValue("");
        this.result.setValue("");
    }

    public MutableLiveData<String> getInput() {
        return input;
    }

    public void setInput(MutableLiveData<String> input) {
        this.input = input;
    }

    public MutableLiveData<String> getResult() {
        return result;
    }

    public void setResult(MutableLiveData<String> result) {
        this.result = result;
    }

    public void ce() {
        result.setValue("");
    }

    public void divide() {
        String cur = input.getValue();
        if (cur.length() == 0 || cur.charAt(cur.length()-1) == '.') {
            expression += "0/";
        } else if (cur.charAt(cur.length()-1) != '+' && cur.charAt(cur.length()-1) != '-' && cur.charAt(cur.length()-1) != '*' && cur.charAt(cur.length()-1) != '/') {
            expression += "/";
        } else {
            expression += "";
        }
        input.setValue(expression);
    }

    public void multiply() {
        String cur = input.getValue();
        if (cur.length() == 0 || cur.charAt(cur.length()-1) == '.') {
            expression += "0*";
        } else if (cur.charAt(cur.length()-1) != '+' && cur.charAt(cur.length()-1) != '-' && cur.charAt(cur.length()-1) != '*' && cur.charAt(cur.length()-1) != '/') {
            expression += "*";
        } else {
            expression += "";
        }
        input.setValue(expression);
    }

    public void c() {
        expression = "";
        input.setValue("");
        result.setValue("");
    }

    public void seven() {
        expression += "7";
        input.setValue(expression);
    }

    public void eight() {
        expression += "8";
        input.setValue(expression);
    }

    public void nine() {
        expression += "9";
        input.setValue(expression);
    }

    public void plus() {
        String cur = input.getValue();
        if (cur.length() == 0 || cur.charAt(cur.length()-1) == '.') {
            expression += "0+";
        } else if (cur.charAt(cur.length()-1) != '+' && cur.charAt(cur.length()-1) != '-' && cur.charAt(cur.length()-1) != '*' && cur.charAt(cur.length()-1) != '/') {
            expression += "+";
        } else {
            expression += "";
        }
        input.setValue(expression);
    }

    public void four() {
        expression += "4";
        input.setValue(expression);
    }

    public void five() {
        expression += "5";
        input.setValue(expression);
    }

    public void six() {
        expression += "6";
        input.setValue(expression);
    }

    public void substract() {
        String cur = input.getValue();
        if (cur.length() == 0 || cur.charAt(cur.length()-1) == '.') {
            expression += "0-";
        } else if (cur.charAt(cur.length()-1) != '+' && cur.charAt(cur.length()-1) != '-' && cur.charAt(cur.length()-1) != '*' && cur.charAt(cur.length()-1) != '/') {
            expression += "-";
        } else {
            expression += "";
        }
        input.setValue(expression);
    }

    public void one() {
        expression += "1";
        input.setValue(expression);
    }

    public void two() {
        expression += "2";
        input.setValue(expression);
    }

    public void three() {
        expression += "3";
        input.setValue(expression);
    }

    public void sqrt() {
        expression += "√";
        input.setValue(expression);
    }

    public void zero() {
        expression += "0";
        input.setValue(expression);
    }

    public void dot() {
        String cur = input.getValue();
        if (cur.charAt(cur.length()-1) == '/' || cur.charAt(cur.length()-1) == '*' || cur.charAt(cur.length()-1) == '+' || cur.charAt(cur.length()-1) == '-' || cur.length() == 0) {
            expression += "0.";
        } else if (cur.charAt(cur.length()-1) == '.'){
            expression += "";
        } else {
            expression += ".";
        }
        input.setValue(expression);
    }

    public void equal() {
        List<String> expressionList = Calculator.expressionToList(expression);
        try {
            List<String> suffixList = Calculator.parseToSuffixExpression(expressionList);
            double calculateResult = Calculator.calculate(suffixList);
            result.setValue(String.valueOf(calculateResult));
        } catch (Exception e) {
            result.setValue("Error");
            Toast.makeText(getApplication(), "表达式输入错误！", Toast.LENGTH_SHORT).show();
        }
    }
}
